package com.zy.controller;

import com.zy.entity.JsonResult;
import com.zy.entity.User;
import com.zy.service.impl.UserServiceImpl;
import com.zy.tools.RedisTool;
import com.zy.tools.appletsTools.PinYinTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/* 修改个人信息
 * @author 小宁
 * @date 2021-04-03 16:01
 */
@Controller
@RequestMapping("/app")
public class ModifyController {
    @Autowired
    UserServiceImpl userService;
    @Autowired
    PinYinTool pinYinTool;
    @Autowired
    RedisTool redisTool;

    /**
     * 根据id  修改姓名、生日、邮箱
     *
     * @param id
     * @param username
     * @param birthday
     * @param email
     * @return com.zy.entity.JsonResult
     * @author 小宁
     * @date 2021-04-03 17:46
     */
    @PostMapping("/upuser")
    @ResponseBody
    public JsonResult upUser(String id, String username, String birthday, String email) {
        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setName(username);
        user.setEmail(email);
        user.setBirthday(birthday);
        user.setLetter(pinYinTool.getPinYinHeadChar(username));
        userService.updateById(user);
        User user1 = userService.getById(id);
        return new JsonResult("200", "修改成功", user1);
    }

    /**
     * 修改手机号
     *
     * @param id
     * @param phone
     * @param email
     * @param code
     * @return com.zy.entity.JsonResult
     * @author 小宁
     * @date 2021-04-13 19:41
     */
    @PostMapping("/upUserPhone")
    @ResponseBody
    public JsonResult upUserPhone(Integer id, String phone, String email, String code) {
        String redisCode = (String) redisTool.get(email);
        if (!code.equals(redisCode)) {
            return new JsonResult("201", "验证码错误", "");
        }

        User user = new User();
        user.setId(id);
        user.setPhone(phone);
        userService.updateById(user);
        User user1 = userService.getById(id);
        user1.setOpenId(null);
        redisTool.del(email);
        return new JsonResult("200", "修改成功", user1);
    }

}